package com.jetpackmvp.base

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MotionEvent
import android.view.inputmethod.InputMethodManager
import androidx.annotation.Nullable
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import com.jetpackmvp.utils.LogUtil
import com.jetpackmvp.widgets.ProcessDialog

abstract class BaseActivity : AppCompatActivity(),
    BaseContract.IBaseView {

    protected var mContext: Context? = null
    protected val alertDialog by lazy {
        ProcessDialog(mContext!!)
    }


//    abstract fun getLayoutContentId(): Int

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        LogUtil.e("base")
//        setContentView(getLayoutContentId())
        mContext = this
//        initView(savedInstanceState)
    }

//    protected abstract fun initView(savedInstanceState: Bundle?)

    protected fun openNewActivity(clazz: Class<*>, @Nullable bundle: Bundle) {
        val intent = Intent(this, clazz)
        bundle.let {
            intent.putExtras(it)
        }
        openNewActivity(intent)
    }

    protected fun openNewActivity(intent: Intent) {
        startActivity(intent)
    }

    protected fun openActivity(action: Class<*>) {
        startActivity(Intent(this, action))
    }


    protected fun changeFragment(fragment: Fragment, first: Boolean, id: Int) {
        val transaction = supportFragmentManager.beginTransaction()
        if (!first) {
            transaction.addToBackStack(null)
        }
        transaction.replace(id, fragment)
        transaction.commitAllowingStateLoss()
    }

    /**
     * 隐藏软键盘
     */
    protected fun setHideSoftInput() {
        val view = window.peekDecorView()
        if (view != null) {
            val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
        }
    }

    override fun onTouchEvent(event: MotionEvent): Boolean {
        if (null != this.currentFocus) {
            val mInputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
            return mInputMethodManager.hideSoftInputFromWindow(this.currentFocus!!.windowToken, 0)
        }
        return super.onTouchEvent(event)
    }

    override fun showProgressDialog() {
        alertDialog.showNormal()
    }

    override fun dismissProgressDialog() {

        alertDialog.dismiss()
    }
}
